package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.MessageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class MessageAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<MessageInfo> messageList;
    private LayoutInflater mLayoutInflater;

    public MessageAdapter(Activity activity, List<MessageInfo> messageList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if(this.messageList == null) {
            this.messageList = new ArrayList<MessageInfo>();
        }
        this.messageList.clear();
        if(messageList != null && messageList.size() > 0) {
            this.messageList.addAll(messageList);
        }
    }

    public void setItems(List<MessageInfo> messageList) {
        if(this.messageList == null) {
            this.messageList = new ArrayList<MessageInfo>();
        }
        this.messageList.clear();
        if(messageList != null && messageList.size() > 0) {
            this.messageList.addAll(messageList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.messageList != null ? this.messageList.size() : 0;
        return count;
    }

    @Override
    public MessageInfo getItem(int i) {
        if(i < 0 || i >= this.messageList.size()) {
            return null;
        }
        return this.messageList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if(view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_message, null, false);
            holder = new ViewHolder();
            holder.imgvAvatar = (ImageView) view.findViewById(R.id.imgvAvatar4ItemMessage);
            holder.txtvName = (TextView) view.findViewById(R.id.txtvName4ItemMessage);
            holder.imgvGender = (ImageView) view.findViewById(R.id.imgvGender4ItemMessage);
            holder.txtvTime = (TextView) view.findViewById(R.id.txtvTime4ItemMessage);
            holder.txtvMessage = (TextView) view.findViewById(R.id.txtvMessage4ItemMessage);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MessageInfo messageInfo = this.messageList.get(position);
        if(messageInfo != null) {
            holder.txtvName.setText(messageInfo.getName());
            holder.txtvTime.setText(messageInfo.getRecentTime());
            holder.txtvMessage.setText(messageInfo.getMessage());
            if(messageInfo.getGender()!= null && messageInfo.getGender().equalsIgnoreCase("1")) {
                holder.imgvGender.setImageResource(R.drawable.ic_gender_male);
            } else {
                holder.imgvGender.setImageResource(R.drawable.ic_gender_female);
            }
            holder.imgvAvatar.setImageResource(R.drawable.ic_message_avatar);
        }
        return view;
    }

    static class ViewHolder {
        ImageView imgvAvatar;
        TextView txtvName;
        ImageView imgvGender;
        TextView txtvMessage;
        TextView txtvTime;
    }
}
